package com.byzk.p2p.admin.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.controller.FileController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.system.dto.Good;
import com.byzk.p2p.admin.system.dto.GoodType;
import com.byzk.p2p.admin.system.service.GoodService;
import com.byzk.p2p.admin.system.service.GoodTypeService;
import com.byzk.p2p.admin.system.vo.GoodVo;

@Controller
@RequestMapping("/admin/good")
public class GoodController extends AdminBaseController {

	@Autowired
	private GoodService goodService;
	@Autowired
	private GoodTypeService goodTypeService;
	@Autowired
	private FileController fileController;
	
	@RequestMapping("/list")
	public String listGood(Model model, @ModelAttribute GoodVo info) {
		PageInfo<GoodVo> pageInfo = goodService.listGood(info);
		model.addAttribute("pageInfo", pageInfo);
		return ftl(Thread.currentThread().getStackTrace());
	}

	/**
	 * 新增或修改商品类型
	 * 
	 * @param good
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/saveOrUpdate")
	public void saveOrUpdate(Model model, @RequestBody Good info) throws Exception {
		try {
			goodService.saveOrUpdate(info);
			super.printJSON(new JsonMsg(-1, "新增成功"));
		} catch (Exception e) {
			super.printJSON(new JsonMsg(1, "操作失败"));
		}
	}
	
	/**
	 * 删除商品类型
	 * @param model
	 * @param id
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public void deleteGood(Model model, @RequestParam("id") Integer id) throws Exception {
		try {
			goodService.delete(id);
			super.printJSON(new JsonMsg(-1, "删除成功"));
		} catch (Exception e) {
			// TODO: handle exception
			super.printJSON(new JsonMsg(1, "删除失败"));
		}
	}
	
	/**
	 * 修改商品之删除商品图片
	 * @param data_info
	 * @throws Exception
	 */
	@RequestMapping("/file/delete")
	public void deleteFile(@RequestParam(value="goodId")String goodId,
			@RequestParam(value="ele")String ele
			) throws Exception {
		System.out.println("goodId=" + goodId + "ele = " + ele);
		
		
		//产品id
		if(goodId == null) {
			printJSON(new JsonMsg(0, "删除失败！"));
		}
		Integer goodId_int = Integer.parseInt(goodId);
		GoodVo goodVo = goodService.detail(goodId_int);
		//产品图片id
		if(ele == null) {
			printJSON(new JsonMsg(0, "删除失败！"));
		}
		if(!goodVo.getThumb().equals(ele)) {
			printJSON(new JsonMsg(0, "删除失败！"));
		}
		goodVo.setThumb("");
		String thumb = ele;		
		//Integer thumb_int = Integer.parseInt(thumb);
		int flag = goodService.saveOrUpdate1(goodVo);
		int flag1 = fileController.deleteResource(thumb);
		if (flag >0 && flag1 > 0) {
			printJSON(new JsonMsg(-1, "删除成功！"));	
		}else{
			printJSON(new JsonMsg(0, "删除失败！"));
		}
	}
	
	/**
	 * 进入修改页
	 * @param model
	 * @param info
	 * @return
	 */
	@RequestMapping("/update")
	public String updateGood(Model model, @RequestParam("id") Integer id) {
		GoodVo goodVo = goodService.detail(id);
		GoodType type = new GoodType();
		type.setLimit(1000);
		List<GoodType> typeList = goodTypeService.listAll(type).getPageList();
		model.addAttribute("typeList",typeList);
		model.addAttribute("good",goodVo);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 进入新增页面
	 * @param model
	 * @param info
	 * @return
	 */
	@RequestMapping("/add")
	public String addGood(Model model) {
		GoodType type = new GoodType();
		type.setLimit(1000);
		List<GoodType> typeList = goodTypeService.listAll(type).getPageList();
		model.addAttribute("typeList",typeList);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
}
